package com.wcl.hencodertest.test14_two_pager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.OverScroller;

public class TwoPagePager extends ViewGroup {

    private float downX;
    private float downY;
    private final ViewConfiguration viewConfiguration;
    private final int minimumFlingVelocity;
    private final int maximumFlingVelocity;
    private int scrollX;
    private boolean scrolling;
    VelocityTracker velocityTracker = VelocityTracker.obtain();
    private final OverScroller overScroller;


    public TwoPagePager(Context context, AttributeSet attrs) {
        super(context, attrs);
        overScroller = new OverScroller(context);
        viewConfiguration = ViewConfiguration.get(context);
        minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
        maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;
        int childTop = 0;
        int childRight = getWidth();
        int childBottom = getHeight();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(childLeft, childTop, childRight, childBottom);
            childLeft += getWidth();
            childRight += getWidth();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);

        boolean b = super.onInterceptTouchEvent(event);
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                scrolling = false;
                downX = event.getX();
                downY = event.getY();
                scrollX = getScrollX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!scrolling) {
                    if (Math.abs(downX - event.getX()) > viewConfiguration.getScaledPagingTouchSlop()) {
                        scrolling = true;
                        getParent().requestDisallowInterceptTouchEvent(true);
                        b = true;
                    }
                }
                break;
        }

        return b;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);


        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                scrollX = getScrollX();
                break;
            case MotionEvent.ACTION_MOVE:
                float swipeX = downX - event.getX() + scrollX;
                if (swipeX > getWidth()) {
                    swipeX = getWidth();
                } else if (swipeX < 0) {
                    swipeX = 0;
                }
                scrollTo((int) swipeX, 0);
                break;
            case MotionEvent.ACTION_UP:
                velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity);
                float xVelocity = velocityTracker.getXVelocity();//获得x滑动的速度
                int scrollX = getScrollX();
                int targetPage = 0;
                if (Math.abs(xVelocity) > minimumFlingVelocity) {
                    targetPage = xVelocity < 0 ? 1 : 0;
                } else {
                    targetPage = (scrollX > getWidth() / 2) ? 1 : 0;
                }
                int scrollDistance = targetPage == 0 ? -scrollX : getWidth() - scrollX;
                overScroller.startScroll(scrollX, 0, scrollDistance, 0);
                postInvalidateOnAnimation();
                break;
        }
        return true;
    }

    @Override
    public void computeScroll() {
        if (overScroller.computeScrollOffset()){
            scrollTo(overScroller.getCurrX(),overScroller.getCurrY());
            postInvalidateOnAnimation();
        }
    }
}
